package com.heima.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.heima.common.constants.ArticleConstants.*;


@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    // 单页最大加载的数字
    private final static int MAX_PAGE_SIZE = 50;
    // 单页最小加载的数字
    private final static int MIN_PAGE_SIZE = 10;

    @Resource
    private ApArticleMapper apArticleMapper;
    @Resource
    private ApArticleContentMapper apArticleContentMapper;
    @Resource
    private ApArticleConfigMapper apArticleConfigMapper;
    @Resource
    private ArticleFreemarkerService articleFreemarkerService;


    @Override
    public ResponseResult<List<ApArticle>> load(Short loadType, ArticleHomeDto dto) {
        //1.校验参数
        int size = Convert.toInt(dto.getSize(), MIN_PAGE_SIZE);
        size = Math.max(size, MIN_PAGE_SIZE);
        size = Math.min(size, MAX_PAGE_SIZE);
        dto.setSize(size);

        //类型参数检验
        if (ObjectUtil.notEqual(loadType, LOADTYPE_LOAD_MORE) && ObjectUtil.notEqual(loadType, LOADTYPE_LOAD_NEW)) {
            loadType = LOADTYPE_LOAD_MORE;
        }

        //文章频道校验
        if (StrUtil.isEmpty(dto.getTag())) {
            dto.setTag(DEFAULT_TAG);
        }

        //时间校验
        if (ObjectUtil.isEmpty(dto.getMaxBehotTime())) {
            dto.setMaxBehotTime(new Date());
        }
        if (ObjectUtil.isEmpty(dto.getMinBehotTime())) {
            dto.setMinBehotTime(new Date());
        }

        //2.查询数据
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, loadType);

        //3.结果封装
        return ResponseResult.okResult(apArticles);
    }

    @Override
    public ResponseResult<Long> saveArticle(ArticleDto dto) {
        //0.检查参数
        if (ObjectUtil.isEmpty(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApArticle article = BeanUtil.toBean(dto, ApArticle.class);

        //1.判断是新增还是更新操作
        if (ObjectUtil.isEmpty(dto.getId())) {
            //说明是新增操作
            //1.进行文章数据保存
            this.save(article);

            //2.进行文章配置保存
            ApArticleConfig apArticleConfig = new ApArticleConfig(article.getId());
            apArticleConfigMapper.insert(apArticleConfig);

            //3.进行文章内容保存
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(article.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        } else {
            //说明是更新操作
            this.updateById(article);
            //进行文章内容修改
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(article.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }
        //进行静态文件生成
        this.articleFreemarkerService.buildArticleToMinIO(article, dto.getContent());

        return ResponseResult.okResult(article.getId());

    }
}
